package cn.com.cennavi.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.spark.api.java.function.Function2;

import scala.Tuple2;
import cn.com.cennavi.bean.DiDiNewData1;
import cn.com.cennavi.bean.DidiQueueStatus2;
import cn.com.cennavi.bean.DifPointTotalMapInfo;







import cn.com.cennavi.bean.GpsInBuffer;

import com.google.common.base.Optional;

public class UpwithstateGPS implements Function2<List<Tuple2<DiDiNewData1,Iterable<DifPointTotalMapInfo>>>, Optional<DidiQueueStatus2>, Optional<DidiQueueStatus2>>{
	//用于存储各个缓冲区的gps点数据
	ArrayList<GpsInBuffer> GpsInBuffer = new ArrayList<GpsInBuffer>();
	//用于存储当前的缓冲区gps点
	GpsInBuffer cacheNow= new GpsInBuffer();
	
	@Override
	public Optional<DidiQueueStatus2> call(List<Tuple2<DiDiNewData1, Iterable<DifPointTotalMapInfo>>> arg0, Optional<DidiQueueStatus2> arg1) throws Exception {
		// TODO Auto-generated method stub
		cacheNow=(cacheNow==null?new GpsInBuffer():cacheNow);
		for(Tuple2<DiDiNewData1, Iterable<DifPointTotalMapInfo>> didiMapPair:arg0){
			
			DiDiNewData1 gpsData=didiMapPair._1;
			Iterable<DifPointTotalMapInfo> mapiNFO=didiMapPair._2;
			
			//首先判断cache缓存中是否有缓存点，如果有：追加到当前gps点前面，如果没有，当前点作为第一个点处理
			
			if(arg1.isPresent()){
				DidiQueueStatus2 oldState=arg1.get();
				
				//添加cache数据到enterbuf中，然后清空cache，此后计算只跟enterbuf有关
				
				boolean oldStateCacheIsNull=judgeOldStateCacheIsNull(oldState);
				if(oldStateCacheIsNull){
					addOldCacheToEnterBuf();
				}else{
					
				}
				clearOldStateCache(oldState);
				
				
				//判断gps点是不是与之前gps点的关系
				if(inSameBuf(oldState,didiMapPair)){
					
				}else{
					//判断是否已经出缓冲区
					if(oldState.car_keepErrorPoint>=2){
						//将这一部分数据保存出来
						addToBufList(GpsInBuffer,cacheNow);
						//保存最后的10个点进cache队列
						
						
					}else{
						
					}
					
					//
				}
			}else{
				
			}
		}
		return arg1;
		
	}

	private void addToBufList(ArrayList<cn.com.cennavi.bean.GpsInBuffer> gpsInBuffer2, cn.com.cennavi.bean.GpsInBuffer cacheNow2) {
		// TODO Auto-generated method stub
		
	}

	private boolean inSameBuf(DidiQueueStatus2 oldState, Tuple2<DiDiNewData1, Iterable<DifPointTotalMapInfo>> didiMapPair) {
		// TODO Auto-generated method stub
		return false;
	}

	private void clearOldStateCache(DidiQueueStatus2 oldState) {
		// TODO Auto-generated method stub
		
	}

	private void addOldCacheToEnterBuf() {
		// TODO Auto-generated method stub
		
	}

	private boolean judgeOldStateCacheIsNull(DidiQueueStatus2 oldState) {
		// TODO Auto-generated method stub
		return false;
	}
	
}